package com.zhong.gang.app.ui.login;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.gson.Gson;
import com.zhong.gang.app.R;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.api.HttpUtils;
import com.zhong.gang.app.db.UserBean;
import com.zhong.gang.app.db.UserUtils;
import com.zhong.gang.app.ui.main.MainActivity;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.OkHttpUtils;
import com.zhong.gang.applib.http.callback.DataCallBack;
import com.zhong.gang.applib.utils.ActivityController;
import com.zhong.gang.applib.utils.CodeTimeTools;
import com.zhong.gang.applib.utils.Md5Utils;

public class LoginPresenter {

    /**
     * 登陆
     * @param phone
     * @param pass
     * @param errPassView
     * @param act
     */
    public final void login(String phone, String pass, View errPassView,LoginAct act){
        OkHttpUtils.post().url(Api.login)
                .addParams("username",phone)
                .addParams("password", Md5Utils.getMd5(pass))
                .build().execute(errPassView.getContext(),"登录",new DataCallBack<BaseDataBean<UserBean>>(act){
            @Override
            public void onResponse(BaseDataBean<UserBean> data) throws Exception {
                super.onResponse(data);
                if (data.getCode() == 200){
                    data.getResult().setRoleStr(new Gson().toJson(data.getResult().getRoleList()));
                    UserUtils.getIntent(act).saveUser(data.getResult());
                    Log.e("==``",UserUtils.getIntent(act).getRoleId()+"--");

                    act.startActivity(new Intent(act, MainActivity.class));
                    act.closeActivity();
                    return;
                }
                errPassView.setVisibility(View.VISIBLE);
            }
        });
    }

    /**
     * 修改密码
     * @param act
     * @param nowPass
     * @param newPass
     */
    public void changePass(ChangePassAct act,String nowPass,String newPass){
        HttpUtils.post(Api.changePass,act)
                .addParams("newPassword", Md5Utils.getMd5(newPass))
                .addParams("password",Md5Utils.getMd5(nowPass))
                .addParams("token", UserUtils.getIntent(act).getUser().getToken())
                .build().execute(act,"修改密码",new DataCallBack<BaseDataBean>(act){
            @Override
            public void onResponse(BaseDataBean data) throws Exception {
                super.onResponse(data);
                act.closeActivity();
            }
        });

    }

    /**
     * 忘记密码
     * @param act
     */
    public void forgetPass(ForgetPassAct act,String userName,String phone,String verifyCode,String pass){
        HttpUtils.post(Api.forgetPass,act)
                .addParams("username",userName)
                .addParams("password",Md5Utils.getMd5(pass))
                .addParams("phone", phone)
                .addParams("verifyCode", verifyCode)
                .build().execute(act,"忘记密码",new DataCallBack<BaseDataBean>(act){
            @Override
            public void onResponse(BaseDataBean data) throws Exception {
                super.onResponse(data);
                act.closeActivity();
            }
        });
    }

    /**
     * 发送验证码
     * @param act
     * @param phone
     */
    public void sendCode(ForgetPassAct act, String phone, TextView textView){
        HttpUtils.post(Api.sendCode,act)
                .addHeader("X-HEADER-ROLE-ID","5")
                .addParams("phone",phone)
                .addParams("type", "3")
                .build().execute(act,"发送验证码",new DataCallBack<BaseDataBean>(act){
            @Override
            public void onResponse(BaseDataBean data) throws Exception {
                super.onResponse(data);
                CodeTimeTools.setTextColor(R.color.gray3,R.color.white);
                CodeTimeTools.setBackColor(R.drawable.bg_blue_radia3,R.drawable.bg_gray_radia3);
                CodeTimeTools timeTools = new CodeTimeTools(act,textView,60000,1000);
                timeTools.start();
            }
        });

    }


    public void loginOut(MineAct act){
        HttpUtils.post(Api.loginOut,act)
                .addParams("token", UserUtils.getIntent(act).getUser().getToken())
                .build().execute(act,"退出登录",new DataCallBack<BaseDataBean>(act){
            @Override
            public void onResponse(BaseDataBean data) throws Exception {
                super.onResponse(data);
                if (data.getCode() == 200){
                    UserUtils.getIntent(act).getUserDao().deleteAll();
                    act.openActivity(LoginAct.class);
                    ActivityController.getInstance().closeAct(MineAct.class);
                    ActivityController.getInstance().closeAct(MainActivity.class);
                }
            }
        });
    }
}
